const express = require("express");
const cookieParser = require("cookie-parser");
const favicon = require("serve-favicon");
const morgan = require("morgan");
const { setPath } = require("../utils/utils");
const { logger, appRoot, cookieKey } = require("../config/config.js");
const htmlMinifier = require("html-minifier");

module.exports = function (app) {
  //日志
  app.use(morgan(logger.level));

  // favicon 图标
  app.use(favicon(setPath(appRoot, "public/favicon.ico")));

  //cookie
  app.use(cookieParser(cookieKey));

  //解析接口 json & url
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));

  //配置模板引擎
  app.set("view options", {
    debug: process.env.NODE_ENV !== "prd",
    cache: process.env.NODE_ENV == "prd",
    minimize: true,
    // HTML 压缩器。仅在 NodeJS 环境下有效
    //htmlMinifier: htmlMinifier,

    // HTML 压缩器配置。参见 https://github.com/kangax/html-minifier
    // htmlMinifierOptions: {
    //   collapseWhitespace: true,
    //   minifyCSS: true,
    //   minifyJS: true,
    //   includeAutoGeneratedTags: true,
    //   removeAttributeQuotes: true,
    //   removeComments: true,
    //   removeRedundantAttributes: true,
    //   removeScriptTypeAttributes: true,
    //   removeStyleLinkTypeAttributes: true,
    //   sortClassName: true,
    //   useShortDoctype: true,
    //   collapseWhitespace: true,
    //   // 运行时自动合并：rules.map(rule => rule.test)
    //   ignoreCustomFragments: [],
    // },
  });
  app.set("view engine", "html");
  app.set("views", setPath(appRoot, "view"));
  app.engine(".html", require("express-art-template"));

  //使用静态资源
  app.use("/public", express.static(setPath(appRoot, "public")));
};
